var http = require( '../../utils/util' )
import { restUserAddress, restUserAddressDelete, restUserAddressDefault } from '../../api/user.js'
var app = getApp()


Page({ 
  data: {
    items: [
      {name: 'CHN', value: '默认地址', checked: 'true'},
    ],
	infodata: []
  },
  
  onLoad: function() {
     this.getUserAddress();
  },
  
  getUserAddress: function() {
	  var that = this;
	  var userId = app.globalData.wxlogin.userId;
	restUserAddress({'userId': userId}).then( res =>{
			console.log(res)
			if(res.code == 0){
				that.setData( { infodata: res.data });
			}
	} );  
  },
  
  add: function(){
	wx.navigateTo({
		url: "/pages/address/addAddress",
	});  
  },
  edit: function(e){
	  var idx = e.target.dataset.idx;
	  wx.navigateTo({
	  	url: "/pages/address/addAddress?params="+JSON.stringify(this.data.infodata[idx])
	  });  
  },
  delete: function(e){
	  var that = this;
	  var id = e.target.dataset.id;
	  var userId = app.globalData.wxlogin.userId;
	  wx.showModal({
		  title: "确定删除吗?",
		  showCancel: true,
		  cancelText: '取消',
		  confirmText: '确定',
		  success: function(res) {
			  if(res.confirm){
				  restUserAddressDelete({userId: userId, addressId: id}).then( res =>{
				  		console.log(res)
				  		if(res.code == 0){
							wx.showToast({
								title: "删除成功!",
								icon: "success"
							});
				  			that.getUserAddress();
				  		}
				  } );
			  }
		  },
		  
		  
	  });
	  
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  		this.getUserAddress();
  },
  
  
  radioChange: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
	restUserAddressDelete({userId: userId, addressId: e.detail.value}).then( res =>{
		console.log(res)
		if(res.code == 0){
			wx.showToast({
				title: "设置成功!",
				icon: "success"
			});
			that.getUserAddress();
		}
	} );
  },
})